<template>
  <div class="layout-all">
    <control :config="config" />
    <div class="layout-header" style="float:left">
      <router-link to="./form/">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-left:0px"
        >添加</el-button>
      </router-link>
    </div>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%;margin-top:50px"
      >
        <el-table-column align="center" prop="id" label="序号" width="70" :fixed="isMobile()?'left':undefined"/>
        <el-table-column align="center" prop="title" label="优惠券名" width="150" />
        <el-table-column prop="type" label="类型" width="100">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.type == '0'?'primary':'success'"
              disable-transitions
            >{{type_arr[scope.row.type]}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="cate_t1" label="系统分类" width="180" >
          <template slot-scope="scope">
            {{scope.row.cate_t1 !== null ? scope.row.cate_t1.title : ""}} 
            {{scope.row.cate_t2 !== null ? '/'+scope.row.cate_t2.title : ""}}
            {{scope.row.cate_t3 !== null ? '/'+scope.row.cate_t3.title : ""}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="shop_cate_t1.title" label="商铺分类" width="100" />
        <el-table-column align="center" prop="number" label="金额" width="100" />
        <el-table-column align="center" prop="min_price" label="使用最小金额" width="110" />
        <el-table-column align="center" prop="period" label="有效期/天" width="120" />
        <!-- <el-table-column prop="grant" label="发送类型" width="120">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.grant === 0"
              type="primary"
              disable-transitions
            >新用户注册</el-tag>
            <el-tag
              v-if="scope.row.grant === 1"
              type="success"
              disable-transitions
            >下单赠送</el-tag>
            <el-tag
              v-if="scope.row.grant === 2"
              type="info"
              disable-transitions
            >按用户发放</el-tag>
            <el-tag
              v-if="scope.row.grant === 3"
              type="warning"
              disable-transitions
            >免费领取</el-tag>
            <el-tag
              v-if="scope.row.grant === 4"
              type="danger"
              disable-transitions
            >线下发放</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column align="center" prop="period" label="有效期/天" width="100" />
        <el-table-column align="center" prop="create_num" label="发放数量" width="100" />
        <el-table-column align="center" prop="send_num" label="已领取数量" width="100" />
        <el-table-column align="center" prop="use_num" label="已使用数量" width="100" />
        <el-table-column align="center" prop="status" label="是否启用" width="80">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.status === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id,'status' ,scope.row.status)"
            >{{ scope.row.status === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="is_register_give" label="注册赠送" width="80">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.is_register_give === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id,'is_register_give' ,scope.row.is_register_give)"
            >{{ scope.row.is_register_give === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="desc" label="描述" />
        <el-table-column
          align="center"
          prop="telephone"
          :fixed="isMobile()?undefined:'right'"
          width="240"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="success" @click="show_code(scope.row.id)" size="small" icon="el-icon-view" v-if="scope.row.type==1"/>
            <router-link :to="'./form/' + scope.row.id" v-if="scope.row.type!=1">
              <el-button type="primary" size="small" icon="el-icon-edit" />
            </router-link>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="onDel(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
     <el-dialog title="未使用优惠券" :visible.sync="show_copy" width="30%" v-if="show_copy">
    <coupon-code :id="id" />
     </el-dialog>
  </div>
</template>

<script>
import control from '../../components/control/control'
import couponCode from '../../components/coupon/couponCode'
export default {
  name:'coupon_index',
  components: { control,couponCode },
  data() {
    return {
      config: [
        { type: 'text', placeholder: '请输入优惠券名', field: 'title', val: '' }
      ],
      value: '',
      searchWhere: {
        title: ''
      },
      tableData: [],
      type_arr: [],
      total: 0,
      id: 0,
      current_page: 1,
      show_copy: false,
      per_page: 14
    }
  },
  activated(){
    if(this.$getUpdate()){
      this.onSearch()
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    // 查询
    handleSizeChange(val) {
      this.onSearch()
    },
    show_code(id) {
      this.id=id
      this.show_copy=true
    },
    handleCurrentChange(val) {
      this.current_page = val
      this.onSearch()
    },
    onSearch() {
      const form = {}
      form.page = this.current_page
      form.map = this.searchWhere
      this.get('/shop/Coupon/index', form, 'POST').then(e => {
        this.type_arr = e.type_arr
        this.tableData = e.list.data
        this.per_page = e.list.per_page
        this.total = e.list.total
        this.current_page = e.list.current_page
      })
    },
    // 添加
    onAdd() {},
    // 编辑
    onEdit(id) {
      console.log(id)
    },
    // 修改状态
    change_status(id, field, val) {
      const form = {}
      form.id = id
      form.field = field
      val === 1 ? form.val = 0 : form.val = 1
      this.get('/shop/Coupon/update', form, 'post').then(e => {
        this.$msg_ok('success', '修改成功')
        this.onSearch()
      })
    },
    // 删除
    onDel(id) {
      this.$msg_confirm().then(e => {
        if (e) {
          this.get('/shop/Coupon/del?id=' + id, this.form, 'POST').then(e => {
            this.onSearch()
          })
        }
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 20px;
}
</style>>
